using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MShop.Core.Domain;
using MShop.Web.Mvc.Filters;
using MShop.Core.Util;
using MShop.Web.Mvc.HttpModule;
using MShop.Core.DataAccess;
using MShop.Core.Service.Membership;
using MShop.Core;


namespace MShop.Web.Controllers
{
    //[CompressFilter]
    [HandleError]
    public class ControllerBase : Controller, IAuthorizationFilter
    {

        public ControllerBase()
        {
            
        }

        public ControllerBase(IShopContext shopContext,IDictDao dictDao)
        {
            this.ShopContext = shopContext;
            this.DictDao = dictDao;
        }

        public  IShopContext ShopContext
        {
            get;
            set;
        }



        public IDictDao DictDao
        {
            get;
            set;
        }

        //public IQueryable<Menu> Menus
        //{
        //    get
        //    {
        //        return DataContext.Menu.Where(x => x.ParentId == null);
        //    }
        //}


        //public virtual void BuildMenuTree(string key="menus")
        //{
        //    HashSet<Menu> hashset = new HashSet<Menu>();
        //    int rowIndex = 1;
        //    DataAccessUtil.QueryableSortMenu(DataContext.Menu.AsEnumerable(), ref hashset, ref rowIndex);
        //    ViewData[key] = hashset;
        //}

        //public virtual void BuildDepartmentTree(string key = "departments")
        //{
        //    HashSet<Department> hashset = new HashSet<Department>();
        //    int rowIndex = 1;
        //    DataAccessUtil.QueryableSortDepartment(DataContext.Department.AsEnumerable(), ref hashset, ref rowIndex);
        //    ViewData[key] = hashset;
        //}


        private string TrimTitle(string title)
        {
            if (!string.IsNullOrEmpty(title))
            {
                try
                {
                    if (title.Substring(title.Length - 1, 1) != "-")
                    {
                        return string.Concat(title, "-");
                    }
                }
                catch
                {
                    return title;
                }
                
            }
            return title;
        }

         public virtual void AppendMeta(string title)
        {

            AppendMeta(TrimTitle(title), null, null);
        }

        public virtual void AppendMeta(string title,string keywords)
        {
            AppendMeta(TrimTitle(title), keywords, null);
        }
        /// <summary>
        /// <meta name="keywords" content="<%=ViewData["keywords"] %>" />
        /// <meta name="description" content="<%=ViewData["description"] %>" />
        /// </summary>
        /// <param name="title"></param>
        /// <param name="keywords"></param>
        /// <param name="description"></param>
        public virtual void AppendMeta(string title,string keywords, string description)
        {
            ViewData["Title"] = TrimTitle(title);
            if (!string.IsNullOrEmpty(keywords))
            {
                ViewData["keywords"] = "<meta name=\"keywords\" content=\"" + keywords + "\" />";
            } if (!string.IsNullOrEmpty(description))
            {
                ViewData["description"] = "<meta name=\"description\" content=\"" + description + "\" />";
            }
        }
         

        
    }
}
